<?php
/**
 * admin888 <doan.hut@gmail.com>
 * Enter description: Class : Danh Dach thu da gui 
 * @author admin888
 *
 */
class ThudaguiController extends Zend_Controller_Action
{
    /**
     * Deverloper : admin888 <doan.hut@gmail.com>
     * Desctiption : dung chung cho toan controler
     */ 
    public function init()
    {
        //cache 
          $this->_cache = Zend_Registry::get('cache');
         
    	//view wellcome 
         $auth = Zend_Auth::getInstance();
         if($auth->hasIdentity()){
			$data = $auth->getIdentity();
			$this->view->name = $data->name;
			$this->view->iddangnhap = $data->uid;
			$this->_uid = $data->uid;
			$cd = new Model_Caidat();
			$thongbao = $cd->getthongbao($data->uid);
			$this->view->thongbao = $thongbao;
         }
        //phần lay out trên
	   $this->view->today =$date = new Zend_Date();
       $reponse = $this->getResponse();
       $reponse->insert('top', $this->view->render('index/top.phtml'));
       $this->view->title = " Quản lý thư đã gửi - Email Manager ";  
    }
    /**
     * Derverloper : admin888 <doan.hut@gmail.com>
     * Description : Check dang nhập  
     */ 
	public function preDispatch(){
	    $auth = Zend_Auth::getInstance();
	    if (!$auth->hasIdentity()) {
	        if ($this->_request->getActionName() != 'login') {
	            $this->_redirect('login');
	        }
         
	    }
	}
	/**
	 *admin888 <doan.hut@gmail.com>
	 * Enter description :Lay thu da gui duoi dang list
	 */
	public function indexAction()
	{
		$gt = new Model_Thudagui();
		$params = $this->_getAllParams();
		$keycache = 'thudagui_'.$this->_uid;
		if (isset($params['keytimkiem']) && $params['keytimkiem'] != ''){
			// tim kiem thu da gui
			$tukhoa = $this->_getParam('keytimkiem');
			$dulieu = $gt->getallthudaguitimkiem($this->_uid,1,$tukhoa);
		}else {
			// check cache 
			//if(!$this->_cache->test($keycache)){
				$dulieu = $gt->getallthudagui($this->_uid,1); // chua co cache se load trong db 
			//}else{
			//	$dulieu = $this->_cache->load($keycache); //neu co cache roi se load cache 
			//}
		}
		// phan trang 
		$page = $this->_getParam('page',1); // bắt biến trên trang
		$paginator = Zend_Paginator::factory($dulieu); // gán mảng vào zend_paginator
		$paginator->setCurrentPageNumber($page); // chuyển tới trang
		$paginator->setItemCountPerPage(15); // số bản ghi trên 1 trang
		$this->view->paginator = $paginator; // view số trang 
		// end phân trang               
        $this->view->thudagui = $paginator;
		//$this->_cache->save($dulieu,$keycache); // luu cache $dulieu can cache / thudagui_uid  key moi cache 
		// lay thong tin gui mail cua user
		$guithu = new Model_Guithu ();
		$ch = $guithu->getthongtinguithu ( $this->_uid );
		$this->view->thonntincauhinh = $ch;
		
	}
	/**
	 *admin888 <doan.hut@gmail.com>
	 * Enter description : Xoa 1 thu da gui
	 */
	public function xoathudaguiAction()
	{
		$gt = new Model_Thudagui();
		$idt = $this->_getParam('idthu');
		$uid = $this->_getParam('uid');
		$xoa = $gt->xoathudagui($uid,$idt);// 
		//xoa fie tron thu muc 
		$arrlinkfile = $gt->getalllinkfile($idt);
		foreach ($arrlinkfile as $lik){
			$xoafilethumuc = $gt->xoafilecuathumuc($lik['link_file']);
		}
		$xoafile = $gt->xoafilekemtheothu($idt);// xoa trong db
		
		$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        $this->_helper->layout->disableLayout(); //disable layout
	}
	public function xoaallthudaguiAction()
	{
		$tg = new Model_Thudagui();
		$namgidt = $this->_getParam('idthu');
		$mangdl = explode(',', $namgidt);
		for ($i= 0 ; $i< count($mangdl);$i++){
			//xoa thu 
			$xoathu = $tg->xoathudagui($this->_uid,$mangdl[$i]);
			//xoa file 
			$arrlinkfile = $gt->getalllinkfile($mangdl[$i]);
			foreach ($arrlinkfile as $lik){
			$xoafilethumuc = $gt->xoafilecuathumuc($lik['link_file']);
		    }
		    // xoa trong bd
			$xoathu = $tg->xoafilekemtheothu($mangdl[$i]);
		}
		$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        $this->_helper->layout->disableLayout(); //disable layout
		
	}
	 	
}//end class  